package com.test.ifelse.test.learn;
/*
*   整合的一个工具类，用于判空
*   注意： 依赖org.apache.commons.lang3包
* */
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.*;

class Validate {
    public static boolean notEmpty(long obj) {
        return 0 != obj;
    }

    public static boolean isEmpty(long obj) {
        return 0 == obj;
    }

    public static boolean notEmpty(int obj) {
        return 0 != obj;
    }

    public static boolean isEmpty(int obj) {
        return 0 == obj;
    }

    public static boolean notEmpty(Object obj) {
        return null != obj;
    }

    public static boolean isEmpty(Object obj) {
        return null == obj;
    }

    public static boolean notEmpty(Collection<?> obj) {
        return null != obj && !obj.isEmpty();
    }

    public static boolean isEmpty(Collection<?> obj) {
        return null == obj || obj.isEmpty();
    }

    public static boolean notEmpty(Map<?, ?> obj) {
        return null != obj && !obj.isEmpty();
    }

    public static boolean isEmpty(Map<?, ?> obj) {
        return null == obj || obj.isEmpty();
    }

    public static boolean notEmpty(File file) {
        return null != file && file.exists();
    }

    public static boolean isEmpty(File file) {
        return null == file || !file.exists();
    }

    public static boolean notEmpty(Object[] obj) {
        return null != obj && 0 < obj.length;
    }

    public static boolean isEmpty(Object[] obj) {
        return null == obj || 0 == obj.length;
    }

    public static boolean notEmpty(Set<?> obj) {
        return null != obj && !obj.isEmpty();
    }

    public static boolean isEmpty(Set<?> obj) {
        return null == obj || obj.isEmpty();
    }

    /*
     *  当执行的判断为false，进行异常抛出处理
     * */
    public Validate falseThrow(boolean bool, String message, Object... params) {
        if (!bool) {
            checkParamsThrows(message, params);
        }
        return this;
    }

    public Validate isNotBlank(String str, String message, Object... params) {
        if (StringUtils.isBlank(str)) {
            checkParamsThrows(message, params);
        }
        return this;
    }

    public Validate isNotEmpty(Object str, String message, Object... params) {
        if (isEmpty(str)) {
            checkParamsThrows(message, params);
        }
        return this;
    }

    public Validate isNotEmpty(Collection<?> collec, String message, Object... params) {
        if (isEmpty(collec)) {
            checkParamsThrows(message, params);
        }
        return this;
    }

    public Validate isNotEmpty(String str, String message, Object... params) {
        isNotBlank(str, message, params);
        return this;
    }

    /**
     * 非空或者非空字符串
     */
    public static boolean notEmptyOrBlank(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof CharSequence) {
            return StringUtils.isNotBlank((CharSequence) obj);
        } else if (obj instanceof Collection) {
            return notEmpty((Collection<?>) obj);
        } else if (obj instanceof Map) {
            return notEmpty((Map<?, ?>) obj);
        } else if (obj instanceof Object[]) {
            return notEmpty((Object[]) obj);
        }
        return true;
    }

    //抽取出来的部分进行封装
    public void checkParamsThrows(String message, Object... params) {
        if (notEmpty(params)) {
            message = String.format(message, params);
        }
        throw new RuntimeException(message);
    }

    public static void main(String[] args) {
        Validate validate = new Validate();
        //构造条件
        String str = null;
        Object user = null;
        Date date = null;

        validate.isNotEmpty(str,"str1不能为空");
        validate.isNotEmpty(user,"user不能为空");
        validate.isNotEmpty(date,"date不能为空");
        validate.falseThrow(2==1,"条件不等，抛出异常");

        //第二种处理方法
        String str1 = Optional.ofNullable(str).orElseThrow(() -> new RuntimeException("str不能为空"));

        //第三种处理方法(设置默认值)
        String str2 = Optional.ofNullable(str).orElse("默认值");
    }
}

